package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.enums.BusinessIdentifyingEnum;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyWeatherQuery;
import com.zl.tby.model.TbyWeather;
import com.zl.tby.repository.TbyWeatherRepo;
import com.zl.tby.service.ITbyFileBusinessService;
import com.zl.tby.service.ITbyWeatherService;
import com.zl.tby.vo.TbyWeatherForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.DbContext;
import org.noear.wood.DbTableQuery;
import org.noear.wood.IPage;
import org.noear.wood.MapperWhereQ;
import org.noear.wood.annotation.Db;
import org.noear.wood.ext.Act1;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 天气接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyWeatherServiceImpl implements ITbyWeatherService {

    @Db
    private TbyWeatherRepo tbyWeatherRepo;
    @Inject
    private ITbyFileBusinessService tbyFileBusinessService;
    @Db
    private DbContext db;

    @Override
    @Tran
    public R<TbyWeather> add(TbyWeatherForm form) {
        TbyWeather entity = form.createEntity();
        entity.setCreateBy(-1L);
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyWeatherRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyWeather> update(TbyWeatherForm form) {
        TbyWeather entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(-1L);
        entity.setUpdateTime(LocalDateTime.now());
        tbyWeatherRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public int delete(Long id) {
        return tbyWeatherRepo.deleteById(id);
    }

    @Override
    public TbyWeather get(Long id) {
        return tbyWeatherRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyWeather> page(TbyWeatherQuery query) {
        IPage<TbyWeather> iPage = tbyWeatherRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getStartDate()), "weather_date >=", query.getStartDate() + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(query.getEndDate()), "weather_date <=", query.getEndDate() + " 23:59:59");
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyWeatherForm> list(TbyWeatherQuery query) {
        DbTableQuery dbTableQuery = db.table("tby_weather").whereTrue();
        if (StringUtils.isEmpty(query.getType())) {
            return new ArrayList<>();
        }
        if (query.getDateArray() != null && query.getDateArray().size() > 0) {
            dbTableQuery.andIn("weather_date", query.getDateArray());
        }
        if (query.getType().equals("today")) {
            // 今日天气
            dbTableQuery.andEq("weather_date", ToolsUtil.dateUtil2String(new Date(System.currentTimeMillis()), "yyyy-MM-dd") + " 00:00:00");
        }
        try {
            List<TbyWeather> dbList = dbTableQuery.selectList("*", TbyWeather.class);
            List<TbyWeatherForm> resultList = new ArrayList<>();
            dbList.forEach(item -> {
                resultList.add(TbyWeatherForm.convert(item));
            });
            return resultList;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ArrayList<>();
    }

}
